package com.woniuxy.handler;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.woniuxy.pojo.PageInfo;
import com.woniuxy.pojo.Student;
import com.woniuxy.service.HouseService;
import com.woniuxy.service.StudentService;
import lombok.Data;

@Data
@Controller
@RequestMapping("/student")
public class StudentHandler {
	@Resource
	private StudentService studentService;
    @Resource
    private HouseService houseService;

	// 分页显示学生
    @RequestMapping("/showAllByPage")
    @ResponseBody
    public ModelAndView ShowAllByPage(int pageNumber) {
        ModelAndView modelAndView = new ModelAndView();
        PageInfo pageInfo = studentService.ShowAllByPage(pageNumber);
        modelAndView.addObject("PageInfo", pageInfo);
        modelAndView.setViewName("/");
        return modelAndView;
	}

	// 多条件查学生
    @RequestMapping("/select")
    @ResponseBody
	public PageInfo select(Student student, int pageNumber) {
		return studentService.select(student, pageNumber);

	}

    // 添加学生
    @RequestMapping("/add")
    @ResponseBody
    public String add(Student student) {
        String flag = "fail";
        System.out.println(student);
        int userNumber = studentService.addUserByStudent(student);
        if (userNumber != 0) {
            int i = studentService.add(student, userNumber);
            if (i != 0) {
                flag = "success";
            }
        } else {
            flag = "用户已存在";
        }
        // 返回0表示学生已存在
        return flag;
    }

    // 退房
    @RequestMapping("/outHouse")
    @ResponseBody
    public int outHouse(int house_id) {
        return houseService.outHouse(house_id);
    }
    // 换房
    @RequestMapping("/updateStudentHouse")
    @ResponseBody
    public int updateStudentHouse(int oldHouse_id, int newHouse_id, int student_id) {
        int i = studentService.updateStudentHouse(oldHouse_id, newHouse_id, student_id);
        return i;

    }

    // 查看学生详细信息
    @RequestMapping("/findStudentById")
    @ResponseBody
    public Student findStudentById(int student_id) {
        return studentService.findStudentById(student_id);
    }


    // test
    @RequestMapping("/test")
    @ResponseBody
    public ModelAndView test() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("test", 1);
        return modelAndView;
    }
}
